package 作业;

import java.util.Random;
import java.util.Scanner;

public class 验证码 {
    public static void main(String[] args) {
        //接收这个字符串
        String code = getIndex();

        System.out.println(code);
        Scanner sc = new Scanner(System.in);

        //boolean b = true;

        while (true) {
            System.out.println("请输入验证码：");
            String next = sc.nextLine();
            if (code.equals(next)) {
                System.out.println("输入正确");
                break;
            } else {
                System.out.println("输入错误  请重新输入");
            }
        }

        //输出语句
    }

    public static String getIndex() {
        //字符串库
        String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        //声明随机数变量
        Random r = new Random();
        //定义code的初始化值
        String code = "";
        //for循环 循次4次 将四次获取的字符串拼接起来
        for (int i = 0; i < 4; i++) {
            //用随机数获取到字符串的随机数
            int index = r.nextInt(s.length());
            //获取到这个字符并将这4个字符拼接起来
            code += s.charAt(index);
        }
        //返回这个字符串
        return code;
    }
}
